package com.wwwarehouse.contract.adjust_contract;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.MessageCenterPushBean;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * Creator by zhenwang.xiang on 2017/10/23 10:11
 * Description: 订单终止详情
 */

@Route(path = ContractConstant.PATH_CONFIRM_TERMINATE_ORDERFRAGMENT)
public class ConfirmTerminateOrderFragment extends BaseFragment implements View.OnClickListener {
    private TextView mOrderDetial;
    private Button mBtnConfirm;
    private TextView mContractTime, mStopName;
    private ArrayList<String> mBusinessIdList;
    private String mContractUkid;
    private String mStopId;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_confirm_terminate_order, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        mOrderDetial = $(R.id.tv_order_detial);
        mBtnConfirm = $(R.id.btn_confirm);
        mContractTime = $(R.id.tv_contractTime);
        mStopName = $(R.id.tv_stop_name);

        mBtnConfirm.setOnClickListener(this);
        mOrderDetial.setOnClickListener(this);

        if (getArguments() != null) {
            MessageCenterPushBean.MsgBean.FeatureBean featureBean = (MessageCenterPushBean.MsgBean.FeatureBean) getArguments().getSerializable(Constant.KEY_MESSAGE_CENTER_FEATURE_DETAILS);
            if (featureBean != null) {
                mContractUkid = featureBean.getContractUkid();
                mStopId = featureBean.getTerminatedContract();
            } else {
                mContractUkid = getArguments().getString("contractUkid");
                mBusinessIdList = getArguments().getStringArrayList("businessIds");
            }
        }
        requestHttp();
    }

    private void requestHttp() {
        Map<String, Object> map = new HashMap<>();
        map.put("contractUkid", mContractUkid);
        NoHttpUtils.httpPost(ContractConstant.GET_INFLUENCETASK_DETAILS, map, mOnResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            if ("0".equals(commonClass.getCode())) {
                switch (what) {
                    case 0:
                        if (commonClass.getData() != null) {
                            JSONObject object = JSON.parseObject(commonClass.getData().toString());
                            String contractTime = object.getString("contractTime");
                            String stopName = object.getString("stopName");

                            mContractTime.setText(Html.fromHtml(StringUtils.getResourceStr(mActivity, R.string.confrim_terminate_order_contracttime, stopName, contractTime)));
                            mStopName.setText(StringUtils.getResourceStr(mActivity, R.string.confrim_terminate_order_stopname, stopName));
                        }
                        break;
                }
            } else {
                toast(commonClass.getMsg());
            }
        }

        @Override
        public void onFailed(String result, int what) {
        }

        @Override
        public void onFinish(int what) {

        }
    };

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ConfirmTerminateOrderFragment) {
            mActivity.setTitle(getString(R.string.confirmtermination_order_title));
        }
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_confirm) {
            popFragment();

        } else if (i == R.id.tv_order_detial) {
            ConstractOrderDetailsFragment orderDetailsFragment = new ConstractOrderDetailsFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("flag", 1);
            bundle.putStringArrayList("businessIds", mBusinessIdList);
            bundle.putString("stopId", mStopId);
            bundle.putString("contractUkid", mContractUkid);
            orderDetailsFragment.setArguments(bundle);
            pushFragment(orderDetailsFragment);

        }
    }
}
